<template>
  <div class="homeBrand">
    <div class="homeBrand_box">
      <div class="homeBrand_top">
        <div class="homeBrand_title">
          <el-icon><Search /></el-icon>
          <span class="homeBrand_title_content">筛选搜索</span>
        </div>
        <div class="homeBrand_reset_button">
          <el-button>重置</el-button>
          <el-button type="primary" @click="handlerFilter">查询搜索</el-button>
        </div>
      </div>
      <div class="homeBrand_bottom">
        <span class="homeBrand_input_label">品牌名称：</span>
        <div class="homeBrand_input_box">
          <el-input type="text" v-model="keyword" placeholder="品牌名称" />
        </div>
        <span class="homeBrand_input_label">推荐状态：</span>
        <div class="homeBrand_input_box">
          <el-select v-model="select" placeholder="全部" style="width: 115px">
            <el-option
              v-for="item in props1"
              :key="item.value"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
    </div>
    <div class="homeBrand_tianjin">
      <div class="homeBrand_left">
        <div class="homeBrand_left_icon">
          <el-icon><Tickets /></el-icon>
          <span class="homeBrand_left_icon_item">数据列表</span>
        </div>
      </div>
      <div class="homeBrand_right_btn">
        <el-button size="small" @click="handleRegister">选择品牌</el-button>
      </div>
    </div>
    <!-- 列表 -->
    <div class="homeBrand_table">
      <el-table
        :data="tableList"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="60" />
        <el-table-column prop="id" width="80" label="编号" />
        <el-table-column prop="productName" label="品牌名称" />
        <el-table-column label="是否推荐" width="200">
          <template #default="scope">
            <div>
              <el-switch
                v-model.number="scope.row.recommendStatus"
                :active-value="1"
                :inactive-value="0"
                @change="Hometown(scope.row)"
              /></div
          ></template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="180" />
        <el-table-column label="状态" width="180">
          <template #default="scope">
            <div v-if="scope.row.recommendStatus === 0">未推荐</div>
            <div v-if="scope.row.recommendStatus === 1">推荐中</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button link type="primary" size="small">设置排序</el-button>
            <el-button
              link
              type="primary"
              size="small"
              @click="deleteBer(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="homeBrand_bartend">
        <div class="homeBrand_bartend_left">
          <el-select
            v-model="select"
            placeholder="批量操作"
            style="margin-right: 10px"
          >
            <el-option
              v-for="(item, index) in props2"
              :key="item.value"
              :value="item.value"
            />
          </el-select>
          <el-button type="primary" @click="handletold">确定</el-button>
        </div>
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[5, 10, 15]"
          :small="false"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <HomeEdit
      :visible="modelVisible"
      @cancel="handleCancel"
      @confirm="handleConfirm"
    />
  </div>
</template>

<script setup lang="ts">
import useTable from "@/hooks/useTable";
import * as home from "@/request/modules/home";
import { reactive, ref, type Ref } from "vue";
import { Search, Tickets } from "@element-plus/icons-vue";
import HomeEdit from "./components/HomeEdit.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import type { BrandList, HomeList } from "@/types/global";

// 模态框显隐状态
const modelVisible = ref(false);

const from = ref({});

const keyword = ref("");
const handlerFilter = () => {};

const select = ref("");
const props1 = reactive([{ value: "设为推荐" }, { value: "取消推荐" }]);
const props2 = reactive([
  { value: "设为推荐" },
  { value: "取消推荐" },
  { value: "删除" },
]);
let {
  tableList,
  fetchData,
  handleSizeChange,
  handleCurrentChange,
  pagination,
} = useTable(home.recommendProductList, { keyword: keyword });

// 取消
const handleCancel = () => {
  modelVisible.value = false;
};
const handleConfirm = (ownForm: BrandList) => {
  ElMessageBox.confirm(`此操作将在列表后添加数据`, "提示")
    .then(async () => {
      // 提交修改后的用户数据
      await home.BrandCreate({
        brandId: ownForm.id,
        brandName: ownForm.name,
      });

      // 请求成功关闭模态框
      modelVisible.value = false;
      // 更改后重新请求用户列表数据，更新页面
      fetchData();
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      // 取消
    });
};
// 打开弹出框
const handleRegister = () => {
  modelVisible.value = !modelVisible.value;
};

// 获取里面的id和状态
let brand = ref<number[]>([]);
const handleSelectionChange = (val: any) => {
  console.log(val);
  brand.value = val.map((item) => item.id);
  console.log(brand.value);
};
const recommendStatus1 = ref(1);
const recommendStatus0 = ref(0);

// 批量操作
const handletold = () => {
  console.log(props2[1]);
  if (select.value === "设为推荐") {
    ElMessageBox.confirm("是否要修改推荐状态?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        ElMessage({
          type: "success",
          message: "成功切换",
        });
        await home.Homebrandupdate({
          ids: brand.value,
          recommendStatus: recommendStatus1.value,
        });
        fetchData();
      })
      .catch(() => {
        ElMessage({
          type: "success",
          message: "已取消操作",
        });
        fetchData();
      });
  } else if (select.value === "取消推荐") {
    ElMessageBox.confirm("是否要修改推荐状态?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        ElMessage({
          type: "success",
          message: "成功切换",
        });
        await home.Homebrandupdate({
          ids: brand.value,
          recommendStatus: recommendStatus0.value,
        });
        fetchData();
      })
      .catch(() => {
        ElMessage({
          type: "success",
          message: "已取消操作",
        });
        fetchData();
      });
    return;
  } else if (select.value === "取消推荐") {
    ElMessageBox.confirm("是否要执行删除操作", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        ElMessage({
          type: "success",
          message: "成功删除",
        });
        await home.branddelete(brand.value);
        fetchData();
      })
      .catch(() => {});
  }
};

// 删除操作
const deleteBer = (row: any) => {
  ElMessageBox.confirm("是否要执行删除操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      ElMessage({
        type: "success",
        message: "成功删除",
      });
      await home.branddelete(row.id);
      fetchData();
    })
    .catch(() => {});
};

// 修改recommendStatus状态
const Hometown = (row: any) => {
  ElMessageBox.confirm("是否要修改推荐状态?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      ElMessage({
        type: "success",
        message: "成功切换",
      });
      await home.Homebrandupdate({
        ids: row.id,
        recommendStatus: row.recommendStatus,
      });
      fetchData();
    })
    .catch(() => {
      ElMessage({
        type: "success",
        message: "已取消操作",
      });
      fetchData();
    });
};
</script>

<style scoped lang="less">
.homeBrand {
  padding: 0 20px;
  margin-top: 20px;
  .homeBrand_box {
    border-radius: 4px;
    border: 1px solid #ebeef5;
    background-color: #fff;
    overflow: hidden;
    color: #303133;
    padding: 20px;
    .homeBrand_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .homeBrand_title {
        display: flex;
        align-items: center;
      }
    }
    .homeBrand_bottom {
      margin-top: 15px;
      display: flex;
      align-items: center;
      .homeBrand_input_label {
        display: inline-block;
        vertical-align: top;
        font-size: 14px;
        margin-left: 50px;
      }
      .homeBrand_input_box {
        text-align: right;
        vertical-align: middle;
        float: left;
        font-size: 14px;
        color: #606266;
        line-height: 40px;
        padding: 0 12px 0 0;
        box-sizing: border-box;
      }
    }
  }
  .homeBrand_tianjin {
    margin: 20px 0;
    padding: 20px;
    border-radius: 4px;
    border: 1px solid #ebeef5;
    background-color: #fff;
    overflow: hidden;
    color: #303133;
    transition: 0.3s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .homeBrand_left {
      .homeBrand_left_icon {
        display: flex;
        align-items: center;
      }
    }
  }
  .homeBrand_bartend {
    display: flex;
    justify-content: space-between;
    margin: 20px 0;
  }
}
</style>
